package com.seckillpro.seckill_pro.controller;

import cn.hutool.core.util.ObjectUtil;
import com.seckillpro.seckill_pro.exception.GlobalException;
import com.seckillpro.seckill_pro.pojo.TUser;
import com.seckillpro.seckill_pro.service.ITUserService;
import com.seckillpro.seckill_pro.utils.CookieUtil;
import com.seckillpro.seckill_pro.utils.MD5Util;
import com.seckillpro.seckill_pro.utils.UUIDUtil;
import com.seckillpro.seckill_pro.utils.ValidatorUtil;
import com.seckillpro.seckill_pro.vo.LoginVo;
import com.seckillpro.seckill_pro.vo.RegisterVo;
import com.seckillpro.seckill_pro.vo.RespBean;
import com.seckillpro.seckill_pro.vo.RespBeanEnum;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 登录
 *
 * @Package: com.SecKillpro.SecKill_pro.controller
 * @ClassName: LoginController
 * @Author: lyx
 * @CreateTime: 2021/3/3 10:59
 * @Description:
 */

@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private ITUserService itUserService;

    /**
     * 跳转登录页面
     *
     * @return
     */
    @GetMapping("/toLogin")
    public String toLogin() {
        return "login";
    }


    /**
     * @param loginVo
     * @return
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public RespBean doLogin(@Valid LoginVo loginVo,  Model model,HttpServletRequest request, HttpServletResponse response) {

        model.addAttribute("mo",loginVo.getMobile());
        return itUserService.doLogin(loginVo, request, response);
    }

    @RequestMapping("/doRegister")
    @ResponseBody
    public RespBean doRegister(@Valid RegisterVo registerVo, HttpServletRequest request, HttpServletResponse response) {

        return itUserService.doRegister(registerVo, request, response);
    }

    @RequestMapping("/out")
    public String out(HttpServletRequest request, HttpServletResponse response) {

        CookieUtil.deleteCookie(request, response, "tUserTicket");
        return "login";
    }
}
